package controller

import (
	"github.com/gin-gonic/gin"
	"shop-bean/request"
	"shop-common/library/serializer"
	"shop-common/utils/contextArgs"
	"shop-common/utils/pagehelper"
	"shop-service/business"
)

type ShippingTemplatesController struct {
}

// @Tags 运费模板
// @description: 保存
// @Summary 保存
// @Security ApiKeyAuth
// @function: Save
// @Accept  json
// @Produce  json
// @Param data body request.ShippingTemplatesRequest true "请求参数"
// @Success 200 {object} string "{"code":0," result":{}, "message":"操作成功", "time":1618493448}"
// @Router //shipping/templates [post]
func (c *ShippingTemplatesController) Save(ctx *gin.Context) {
	form := request.ShippingTemplatesRequest{}
	if err := serializer.Verify(ctx, &form); err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	err := business.ServiceImpl().ShippingTemplatesService.Create(&form)
	if err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}
	serializer.Success(ctx, serializer.SUCCESS)
}

// @Tags 运费模板
// @description: 运费模板分页
// @Summary 分页
// @Security ApiKeyAuth
// @function: Page
// @Accept  json
// @Produce  json
// @Param current query string false "当前页，默认1"
// @Param size query string false "每页大小，默认10"
// @Param name query string false "模糊查询 模板名称"
// @Param status query int true "status"
// @Success 200 {object} string "{"code":0," result":{}, "message":"操作成功", "time":1618493448}"
// @Router /shipping/templates/page [get]
func (c *ShippingTemplatesController) Page(ctx *gin.Context) {
	form := pagehelper.SearchPageRequest{}
	if err := serializer.Verify(ctx, &form); err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	page, err := business.ServiceImpl().ShippingTemplatesService.FindPage(&form)
	if err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	serializer.SuccessData(ctx, "", page)
}

// @Tags 运费模板
// @description: 运费模板列表
// @Summary 分页
// @Security ApiKeyAuth
// @function: List
// @Accept  json
// @Produce  json
// @Param status query int true "status"
// @Success 200 {object} string "{"code":0," result":{}, "message":"操作成功", "time":1618493448}"
// @Router /shipping/templates/list [get]
func (c *ShippingTemplatesController) List(ctx *gin.Context) {
	list, err := business.ServiceImpl().ShippingTemplatesService.FindSimple()
	if err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	serializer.SuccessData(ctx, "", list)
}

// @Tags 运费模板
// @description: 运费模详情
// @Summary 运费模详情
// @Security ApiKeyAuth
// @function: Info
// @Accept  json
// @Produce  json
// @Param id path int true "id"
// @Success 200 {object} string "{"code":0," result":{}, "message":"操作成功", "time":1618493448}"
// @Router /shipping/templates/info/{id} [get]
func (c *ShippingTemplatesController) Info(ctx *gin.Context) {
	id, err := contextArgs.GetUriIntPath(ctx, "id")
	if err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	info, err := business.ServiceImpl().ShippingTemplatesService.FindById(id)
	if err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	serializer.SuccessData(ctx, "", info)
}

// @Tags 运费模板
// @description: 更新运费模详情
// @Summary 更新
// @Security ApiKeyAuth
// @function: Update
// @Accept  json
// @Produce  json
// @Param data body request.ShippingTemplatesRequest true "请求参数"
// @Success 200 {object} string "{"code":0," result":{}, "message":"操作成功", "time":1618493448}"
// @Router /shipping/templates [put]
func (c *ShippingTemplatesController) Update(ctx *gin.Context) {
	form := request.ShippingTemplatesRequest{}
	if err := serializer.Verify(ctx, &form); err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	if form.Id > 1 {
		serializer.Fail(ctx, "id不能为空")
		return
	}

	err := business.ServiceImpl().ShippingTemplatesService.Update(&form)
	if err != nil {
		serializer.Fail(ctx, serializer.PublicUpdateErr)
		return
	}

	serializer.Success(ctx, serializer.SUCCESS)
}

// @Tags 运费模板
// @description: 删除
// @Summary 删除
// @Security ApiKeyAuth
// @function: Delete
// @Accept  json
// @Produce  json
// @Param data body request.CommonBatchRequest true "请求参数"
// @Success 200 {object} string "{"code":0," result":{}, "message":"操作成功", "time":1618493448}"
// @Router /shipping/templates [delete]
func (c *ShippingTemplatesController) Delete(ctx *gin.Context) {
	form := request.CommonBatchRequest{}
	if err := serializer.Verify(ctx, &form); err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	err := business.ServiceImpl().ShippingTemplatesService.BatchDelete(form.Ids)
	if err != nil {
		serializer.Fail(ctx, serializer.PublicDeleteErr)
		return
	}

	serializer.Success(ctx, serializer.SUCCESS)
}
